# coding:utf-8
import re
from xpinyin import Pinyin
from pywubi import wubi
# print(wubi('我'))

p = Pinyin()


def read_file(filename="") -> list:
    with open(filename, "r", encoding="utf_8") as fr:  # read input file
        return fr.readlines()


def get_lines(lines):
    res = []
    m = {}
    for line in lines:
        if re.match(r"\d+\..+", line):
            # .*?
            s = re.sub(
                r"\(.*\)|(\d+\.)|(\d+)|[,\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]",
                "",
                line,
            )
            # s = s.replace("，","").replace("。","").replace("、","")
            # print(re.match(r"[\u4e00-\u9fa5]",line))
            # print(line,end="")
            # print(s)
            if s not in m:
                res.append(s)
            # else:
            # 	print("re:" + s)
            m[s] = 1
            # print(line)
    return res


if __name__ == "__main__":
    in_filename = "top.txt"
    lines = read_file(in_filename)
    allQ = get_lines(lines)
    # print(allQ)
    res1 = []
    res2 = []
    res3 = []

    test_r1 = {}
    test_r2 = {}
    test_r3 = {}
    for q in allQ:
        # res = p.get_pinyin('汉字转拼音', q[0]) # res = ((q[0],p.get_pinyin(q, "-").split("-")[0]),(q[-2],p.get_pinyin(q, "-").split("-")[-2]))
        s1 = p.get_pinyin(q[0]) + p.get_pinyin(q[1])
        s2 = p.get_pinyin(q[0]) + p.get_pinyin(q[-2])
        s3 = wubi(q[0])[0] + wubi(q[-2])[0]
        s4 = wubi(q[0])
        # print(type(s4),s4[0])

        if s2 in test_r2:
            print("re:" + q, test_r2[s2])

        if s3 in test_r3:
            print("re:" + q, test_r3[s3])

        test_r1[s1] = q
        test_r2[s2] = q
        test_r3[s3] = q

        res1.append(s1)
        res2.append(s2)
        res3.append(s3)
        # print(s1)
        # print(s2)
    print(len(res1), len(set(res1)), 100 - len(set(res1)) / len(res1) * 100, "%")
    print(len(res2), len(set(res2)), 100 - len(set(res2)) / len(res2) * 100, "%")
    print(len(res3), len(set(res3)), 100 - len(set(res3)) / len(res3) * 100, "%")

    print(sum([len(_) for _ in res2]) / len(res2) / 2)
    print(sum([len(_) for _ in res3]) / len(res3) / 2)

    print(res2)
    print(res3)
